<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modellist');
/**
 * AnJoLinA Plugins Model
 */
class AnJoLinAModelanjolinaplugins extends JModelList
{
	
/**
	 * Constructor.
	 *
	 * @param	array	An optional associative array of configuration settings.
	 * @see		JController
	 * @since	1.6
	 */
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
//			$config['filter_fields'] = array(
//				'id', 'a.id',
//				'title', 'a.title',
//				'alias', 'a.alias',
//				'published', 'a.published',
//				'mobilepublished', 'a.mobilepublished',
//				'access', 'a.access', 'access_level',
//				'language', 'a.language',
//				'checked_out', 'a.checked_out',
//				'checked_out_time', 'a.checked_out_time',
//				'created_time', 'a.created_time',
//				'created_user_id', 'a.created_user_id',
//				'lft', 'a.lft',
//				'rgt', 'a.rgt',
//				'level', 'a.level',
//				'path', 'a.path',
//			);
			$config['filter_fields'] = array(
							'id', 'p.id',
							'name', 'p.name',
							'desc', 'p.desc',
							'dir', 'p.dir',
							'activexml', 'p.activexml',
							'activesoap', 'p.activesoap',
							'activejson', 'p.activejson',
							'class', 'p.class',
							'version', 'p.version',
							'author', 'p.author',
							
			);
		}
		
		parent::__construct($config);
	}

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @param	string	An optional ordering field.
	 * @param	string	An optional direction (asc|desc).
	 *
	 * @return	void
	 * @since	1.6
	 */
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app		= JFactory::getApplication();
		$context	= $this->context;

		$extension = $app->getUserStateFromRequest('com_anjolina.anjolinaplugins.filter.extension', 'extension', 'com_anjolina');

		$this->setState('filter.extension', $extension);
		$parts = explode('.',$extension);

		// extract the component name
		$this->setState('filter.component', $parts[0]);

		// extract the optional section name
		$this->setState('filter.section', (count($parts) > 1) ? $parts[1] : null);

		$search = $this->getUserStateFromRequest($context.'.search', 'filter_search');
		$this->setState('filter.search', $search);

//		$level = $this->getUserStateFromRequest($context.'.filter.level', 'filter_level', 0, 'int');
//		$this->setState('filter.level', $level);
//
//		$access = $this->getUserStateFromRequest($context.'.filter.access', 'filter_access', 0, 'int');
//		$this->setState('filter.access', $access);
//
//		$published = $this->getUserStateFromRequest($context.'.filter.published', 'filter_published', '');
//		$this->setState('filter.published', $published);
//		
//		$mobilepublished = $this->getUserStateFromRequest($context.'.filter.mobilepublished', 'filter_mobilepublished', '');
//		$this->setState('filter.mobilepublished', $mobilepublished);
//
//		$language = $this->getUserStateFromRequest($context.'.filter.language', 'filter_language', '');
//		$this->setState('filter.language', $language);

		// List state information.
		parent::populateState('p.name', 'asc');
	}

	/**
	 * Method to get a store id based on model configuration state.
	 *
	 * This is necessary because the model is used by the component and
	 * different modules that might need different sets of data or different
	 * ordering requirements.
	 *
	 * @param	string		$id	A prefix for the store id.
	 *
	 * @return	string		A store id.
	 * @since	1.6
	 */
	protected function getStoreId($id = '')
	{
		// Compile the store id.
		$id	.= ':'.$this->getState('filter.search');
		$id	.= ':'.$this->getState('filter.extension');
//		$id	.= ':'.$this->getState('filter.published');
//		$id	.= ':'.$this->getState('filter.mobilepublished');
//		$id	.= ':'.$this->getState('filter.language');

		return parent::getStoreId($id);
	}

	/**
	 * @return	string
	 * @since	1.6
	 */
	function getListQuery()
	{
		// Create a new query object.
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);
		$user	= JFactory::getUser();

		// Select the required fields from the table.
		$query->select(
			$this->getState(
				'list.select',
				'p.id, p.name, p.desc, p.dir , p.class, p.version, p.author, p.activexml, p.activesoap, p.activejson'
			)
		);
		$query->from('#__ajl_plugins AS p');

	
		
		// Filter by search in title
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'id:') === 0) {
				$query->where('p.id = '.(int) substr($search, 3));
			}
			else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(p.name LIKE '.$search.')');
			}
		}


		// Add the list ordering clause.
		$query->order($db->getEscaped($this->getState('list.ordering', 'p.name')).' '.$db->getEscaped($this->getState('list.direction', 'ASC')));

		//echo nl2br(str_replace('#__','jos_',$query));
		//echo $query;
		
		
		return $query;
	}


}